package com.eastfair.gateway.controller;

import com.eastfair.gateway.dto.BackListDTO;
import com.eastfair.gateway.entity.BackList;
import com.eastfair.gateway.dto.BackListSaveDTO;
import com.eastfair.gateway.dto.BackListUpdateDTO;
import com.eastfair.gateway.dto.BackListPageQuery;
import com.eastfair.gateway.service.BackListService;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.eastfair.boot.controller.SuperController;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eastfair.echo.core.EchoService;
import com.eastfair.core.base.R;
import com.eastfair.gateway.service.GatewayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.eastfair.annotation.annotation.security.PreAuth;

import com.eastfair.boot.request.PageParams;
import com.eastfair.core.context.ContextUtil;


/**
 * <p>
 * 前端控制器
 * 黑名单
 * </p>
 *
 * @author ligang
 * @date 2022-07-30
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/backList")
@Api(value = "BackList", tags = "黑名单")
@PreAuth(replace = "gateway:backList:")
public class BackListController extends SuperController<BackListService, Long, BackList, BackListPageQuery, BackListSaveDTO, BackListUpdateDTO> {


    @Autowired
    GatewayService gatewayService;

    @Override
    public void handlerResult(IPage<BackList> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<BackList> backListList = list.stream().map((map) -> {
            BackList backList = BackList.builder().build();
            //TODO 请在这里完成转换
            return backList;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(backListList));
    }


    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<BackListPageQuery> params) {
        BackListPageQuery pageQuery = params.getModel();
        //分页处理
        if (ContextUtil.getProjectId() != null) {
            pageQuery.setProjectId(ContextUtil.getProjectId());
        }
    }


    /**
     * 添加黑名单
     *
     * @param backListDTO 组织实体
     */
    @ApiOperation(value = "创建节点组", notes = "创建节点组")
    @PostMapping("/executeAddBackList")
    public R<BackList> executeAddBackList(@RequestBody BackListDTO backListDTO) {
        return R.success(baseService.executeAddBackList(backListDTO, backListDTO.getStartTime(), backListDTO.getEndTime()));
    }

    /**
     * 校验用户是否黑名单
     *
     * @param backListDTO 组织实体
     */
    @ApiOperation(value = "校验用户是否黑名单", notes = "校验用户是否黑名单")
    @PostMapping("/verifyUserBackList")
    public R<Boolean> verifyUserBackList(@RequestBody BackListDTO backListDTO) {
        List<BackList> backListList = gatewayService.queryBackList(backListDTO);
        if (backListList == null || backListList.isEmpty()) {
            return R.success(false);
        }
        for (BackList backList : backListList) {
            if (backList.getLimitStart().isBefore(LocalDateTime.now()) && backList.getLimitEnd().isAfter(LocalDateTime.now())) {
                //有效时间的数据
                return R.success(true);
            }
        }
        return R.success(true);
    }

    /**
     * test
     */
    @ApiOperation(value = "test", notes = "test")
    @GetMapping("/test")
    public R test() {
        return R.success("333333333333");
    }
}
